#include "plantshop.h"

PlantShop::PlantShop()
{
    counter = 0;
    time = int(7.0 * 1000 / 33);

    Card *card = nullptr;
    for (int i = 0; i < PlantName.size(); ++i)
    {
        card = new Card(PlantName[i]);
        card->setParentItem(this);
        card->setPos(-157 + 65 * i, -2);
    }
}
void PlantShop::advance(int phase)
{
    if (!phase)
        return;
    update();
    if (++counter >= time)
    {
        counter = 0;
        scene()->addItem(new Sun);
    }
}

void PlantShop::addPlant(int faction,  int xpos, int ypos ,QString s)
{
    if(faction == 1) xpos = 1028;

    QPointF pos(xpos, ypos);
    QList<QGraphicsItem *> items = scene()->items(pos);
    foreach (QGraphicsItem *item, items)
        if (item->type() == Plant::Type)
            return;
    if(Card::map[s]<=6) sun -= Card::cost[Card::map[s]];
    QGraphicsItem *plant = nullptr;
    switch (Card::map[s])
    {
    case 0:
        plant = new SunFlower(2); break;
    case 1:
        plant = new Peashooter; break;
    case 2:
        plant = new CherryBomb; break;
    case 3:
        plant = new WallNut; break;
    case 4:
        plant = new SnowPea; break;
    case 5:
        plant = new PotatoMine; break;
    case 6:
        plant = new Repeater; break;
    case 7:
        plant = new BasicZombie; break;
    case 8:
        plant = new BucketZombie; break;
    case 9:
        plant = new ConeZombie; break;
    case 10:
        plant = new FootballZombie; break;
    case 11:
        plant = new ScreenZombie; break;
    }
    plant->setPos(pos);
    scene()->addItem(plant);
    QList<QGraphicsItem *> child = childItems();
    foreach (QGraphicsItem *item, child)
    {
        Card *card = qgraphicsitem_cast<Card *>(item);
        if (card->text == s)
            card->counter = 0;
    }
}
